package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"xialou_platform/app/requestx"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CourseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCourseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CourseListLogic {
	return &CourseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CourseListLogic) CourseList(in *agentClient.ListReq) (*agentClient.CourseListResp, error) {
	// todo: add your logic here and delete this line
	count := l.svcCtx.CourseModel.FindListCount(l.ctx, in.Keyword, in.Online)
	courses := make([]*agentClient.Course, 0)

	if count > 0 {
		list, err := l.svcCtx.CourseModel.FindList(l.ctx, &requestx.ListReq{
			Page:     in.Page,
			PageSize: in.PageSize,
			Keyword:  in.Keyword,
		}, in.Online)
		if err != nil {
			return nil, err
		}
		for _, v := range list {
			temp := &agentClient.Course{}
			copier.Copy(&temp, v)
			temp.CreateTime = v.CreateTime.Unix()
			temp.UpdateTime = v.UpdateTime.Unix()
			courses = append(courses, temp)
		}
	}
	return &agentClient.CourseListResp{
		List:  courses,
		Total: count,
	}, nil
}
